package com.css.dzj.zfjg.modules.admpunishment.service.impl;

import com.css.dzj.zfjg.common.util.CvaConstants;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.adminspection.entity.JgJgxwCheckActionEntity;
import com.css.dzj.zfjg.modules.admpunishment.mapper.LhjgXwMapper;
import com.css.dzj.zfjg.modules.admpunishment.service.JgJgxwPunishActionService;
import com.css.dzj.zfjg.modules.admpunishment.vo.JgJgxwPunishActionVo;
import com.css.dzj.zfjg.modules.admpunishment.entity.JgJgxwPunishActionEntity;
import com.css.dzj.zfjg.modules.admpunishment.entity.QJgJgxwPunishActionEntity;

import com.css.dzj.zfjg.modules.jcdx.entity.JgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.jcdx.entity.QJgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.taskinfo.vo.rwjgLh;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;


@Service("jgJgxwPunishActionService")
public class JgJgxwPunishActionServiceImpl implements JgJgxwPunishActionService {
    //注入JPAQueryFactory工厂
    @Autowired
    JPAQueryFactory jpaQueryFactory;
    @Autowired
    private LhjgXwMapper lhjgXwMapper;

    @Override
    public QueryResults queryPage(Pageable pageable) {
        QJgJgxwPunishActionEntity q = QJgJgxwPunishActionEntity.jgJgxwPunishActionEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        return  jpaQueryFactory
                .selectFrom(q)
                .where(
                        predicate
                )
                .orderBy(q.createTime.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }


    @Override
    public JgJgxwPunishActionEntity getById(String id) {
        QJgJgxwPunishActionEntity q = QJgJgxwPunishActionEntity.jgJgxwPunishActionEntity;
        JgJgxwPunishActionEntity fetchOne = jpaQueryFactory
                .selectFrom(q)
                .where(q.id.eq(id),
                       q.cdOperation.eq(CvaConstants.UN_DELETED_FLAG)
                )
                .fetchOne();
        return fetchOne;
    }

    @Override
    public rwjgLh findOneById(String actionId) {
        if(StringUtils.isEmpty(actionId)) {
            return null;
        }
        QJgJgxwPunishActionEntity q = QJgJgxwPunishActionEntity.jgJgxwPunishActionEntity;
        JgJgxwPunishActionEntity fetchOne = jpaQueryFactory
                .selectFrom(q)
                .where(q.id.eq(actionId),
                        q.cdOperation.eq(CvaConstants.UN_DELETED_FLAG)
                )
                .fetchOne();
        rwjgLh vo = new rwjgLh();

        if(fetchOne != null && !StringUtils.isEmpty(fetchOne.getSuperviseObject())) {
            QJgLhjgJcdxEntity jcdx = QJgLhjgJcdxEntity.jgLhjgJcdxEntity;
            JgLhjgJcdxEntity dx = jpaQueryFactory
                    .selectFrom(jcdx)
                    .where(jcdx.enterprisecreditcode.eq(fetchOne.getSuperviseObject())
                    )
                    .fetchFirst();
            vo.setJgLhjgJcdxEntity(dx);
        }


        vo.setJgJgxwPunishActionEntity(fetchOne);

        return vo;
    }

    public String save(JgJgxwPunishActionEntity entity){

        entity.setCdTime(new Date());
        lhjgXwMapper.save(entity);
        return null;
    }

    @Override
    public R updateById(String id) {
        try {
            if(!"".equals(id) && id!=null){
                QJgJgxwPunishActionEntity q = QJgJgxwPunishActionEntity.jgJgxwPunishActionEntity;
                jpaQueryFactory.update(q)
                        .set(q.cdOperation,CvaConstants.UN_DELETED_FLAG)
                        .where(q.id.eq(id))
                        .execute();
                return R.ok();
            }else {
                throw new RuntimeException();
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 通过id查询处罚行为信息
     * @param actionId
     * @return
     */
    @Override
    public JgJgxwPunishActionEntity findById(String actionId) {
        return lhjgXwMapper.findById(actionId).get();
    }

    /**
     * 物理删除处罚行为信息
     * @param punishActionEntity
     */
    @Override
    public void physicalDelete(JgJgxwPunishActionEntity punishActionEntity) {
        try {
            lhjgXwMapper.delete(punishActionEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}